<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class departemen extends CI_Controller {
	
	var $objectid = 'departemen';
	
	function __construct(){
		parent::__construct();
		$this->load->model('sd/mdl_departemen');
		//$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$data['objectid'] = $this->objectid;
		$this->load->view('sd/departemen', $data);
	}
	
	function grid(){
		$data = $this->mdl_departemen->get_data();
		echo $this->mdl_departemen->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['objectid'] = $this->objectid;
		
		//$data['kd_subtema'] = '';
		$data['kd_departemen'] = '';
		$data['nama_departemen'] = '';
		$data['urutan'] = '';
		$data['keterangan'] = '';
		
		$this->load->view('sd/departemen_form', $data);
	}
	
	function edit($kode){
		$data['objectid'] = $this->objectid;
		
		$data['kd_departemen'] = $kode;
		$r = $this->mdl_departemen->get_data_edit($kode);
		
		$data['kd_departemen'] = $r->row()->kd_departemen;
		$data['nama_departemen'] = $r->row()->nama_departemen;
		$data['urutan'] = $r->row()->urutan;
		$data['keterangan'] = $r->row()->keterangan;
		//$data['kode_awalan'] = $r->row()->kode_awalan;
		//$data['keterangan'] = $r->row()->keterangan;
        
		
		$this->load->view('sd/departemen_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		//$data['kode_awalan'] = strtoupper($data['kode_awalan']);
		
		# rules validasi form
		$this->form_validation->set_rules('kd_departemen', 'Kode Departemen', 'trim|required|xss_clean');
        $this->form_validation->set_rules('nama_departemen', 'Nama Departemen', 'trim|required|xss_clean');
		$this->form_validation->set_rules('urutan', 'Urutan', 'trim|required|xss_clean');
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');
		$this->form_validation->set_message('required', 'Field %s harus diisi.');
		$this->form_validation->set_message('required', 'Field %s harus diisi.');
		
		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			$data["pesan_error"].= trim(form_error('kd_departemen',' ',' '))==''?'':form_error('kd_departemen',' ',' ').'<br>';
			$data["pesan_error"].= trim(form_error('nama_departemen',' ',' '))==''?'':form_error('nama_departemen',' ',' ').'<br>';
			$data["pesan_error"].= trim(form_error('urutan',' ',' '))==''?'':form_error('urutan',' ',' ').'<br>';
			
		}else{
			if($aksi=="add"){ // add
				//$result = !$this->mdl_departemen->isExistKode($data['nip']);
				//if ($result){
					$result = $this->mdl_departemen->insert($data);
				//}else{
					//$data['pesan_error'] .= 'Sudah terdapat NIP dalam database!';
				//}
			}else { // edit
				//if($data['kode'] == $data['nip']){ // jika kode tidak diubah
					$result=$this->mdl_departemen->update($data);
				// }else{
					// $result = !$this->mdl_departemen->isExistKode($data['nip']);
					// if ($result){
						// $result=$this->mdl_departemen->UpdateOnDb($data);
					// }else{
						// $data['pesan_error'] .= 'Sudah terdapat NIP dalam database!';
					// }
				// }
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_departemen->delete($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
}